$(function () {
    findById();
})

function findById() {
    // let  url="/html/user/userEdit.html?id=3";
    // let id=aa(url)=id=3
    let id = sessionStorage.getItem("productId");
    $.ajax({
        url: "/back/product/findById",
        data: {id: id},
        dataType: 'json',
        type: 'post',
        success: function (data) {
            console.log(data);
            setData(data.model);

        }

    })
}

function setData(data) {
    $("#productName").val(data.productName);
    $("#describe").val(data.describe);
    $("#mold option:selected").val(data.mold);
    $("#level").val(data.level);
    $("#weight").val(data.weight);
    $("#price").val(data.price);

}
function save() {
    // console.log(data);
    let data = new FormData();
    data.append("productId", $("#productId").val());
    data.append("name", $("#productName").val());
    data.append("describe", $("#describe").val());
    data.append("mold", $("#option:selected").val());
    data.append("level", $("#level").val());
    data.append("weight", $("#weight").val());
    data.append("price", $("#price").val());
    // data.append("imgHref", $("#imgHref")[0].files[0]);
    let result = myImgAjax("/product/add", data);
    console.log(result);
    if (result.result == "success") {
        load("/product/productManage.html")
    } else if (result.result == "noLogin") {
        window.open("/html/login.html", "_self");
    } else {

        alert("新增失败");
    }
}
function clearContent() {
    $("#productName").val("");
    $("#describe").val("");
    $("#level").val("");
    $("#weight").val("");
    $("#price").val("");
}
function Adding() {
    let da = {
        productId:sessionStorage.getItem("productId"),
        productName: $("#productName").val(),
        describe: $("#describe").val(),
        mold: $("#mold option:selected").val(),
        level: $("#level").val(),
        weight: $("#weight").val(),
        price: $("#price").val()
    };
    // console.log(data);
    $.ajax({
        url: '/back/product/edit',//对应的是后台servlet的webServlet的注解
        data: da,//da 前端传递到后端的数据
        type: 'post',
        dataType: 'json',//
        success: function (data) {//后端响应给前台的数据

            if (data.result == 'success') {
                findAll();
                alert("添加成功");
                closeWindow();
                // $(".right").load("/html/user/userManage.html")
            }  else {
                alert("添加失败");
            }
        }
    });

}
